/*
 * pdf.java
 *
 * Created on 19 luglio 2005, 19.45
 */

package Pdf;

import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;

import java.sql.*;
import java.util.*;
import DataAccess.Actions;


import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
/**
 *
 * @author andrea
 * @version
 */
public class pdf extends HttpServlet {
    
   

    public void doPost (HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException {
        //processRequest(request, response);
        String sql = request.getParameter("query");
        String titolo = request.getParameter("titolo");
        String driverstring = request.getParameter("driverstring");
        String connectionstring = request.getParameter("connectionstring");
        // step 1: creation of a document-object
        Document document = new Document();
        
        String nome_file = titolo.replace(" ", "_");
        
        //response.setContentType("application/x-download");
        response.addHeader("Content-Disposition", "attachment; filename=\""+ nome_file.replace(",", "_") + ".pdf\"");
        //response.setContentType("application/pdf");
        
        try {
            PdfWriter.getInstance(document, response.getOutputStream());
        } catch (DocumentException de) {
            de.printStackTrace();
            System.err.println("document: " + de.getMessage());
        }
        document.open();
// Inserire connessione a DB e Ciclo
//String sql="select abbonati.nominativo as Nominativo,abbonati.indirizzo as Indirizzo, abbonati.cap as CAP, citta.nome_citta as Comune, province.sigla_provincia as Provincia, tipologie.descrizione_tipologia as Tipologia from abbonati, citta, province, tipologie where abbonati.id_citta = citta.id_citta and citta.id_provincia = province.id_provincia and abbonati.id_tipologia = tipologie.id_tipologia and abbonati.id_tipologia=7";
        
        DataAccess.Actions actions = new DataAccess.Actions();
        
        try {
            //Class.forName("com.mysql.jdbc.Driver");
            Class.forName(driverstring);
            //Connection cn = DriverManager.getConnection ("jdbc:mysql://127.0.0.1/carta?user=root&password=");
            Connection cn = DriverManager.getConnection (connectionstring);
            java.sql.Statement stmt = cn.createStatement();
            //String sql=actions.getCurrentQuery();

            
            
            int RowCount = 0;
            ResultSet rs = stmt.executeQuery(sql);
            
            ResultSetMetaData rsm = rs.getMetaData();
            int col = rsm.getColumnCount();

            Font font_titolo = new Font(Font.HELVETICA, 12, Font.BOLD);
            Font font_sottotitolo =  new Font(Font.HELVETICA, 6, Font.ITALIC);
            Font font_normal = new Font(Font.HELVETICA, 6, Font.NORMAL);
            Font font_bold = new Font(Font.HELVETICA, Font.DEFAULTSIZE, Font.BOLD);
            
            
            PdfPTable table = new PdfPTable(col);
         
            for(int i=1; i<=col; i++) {
                table.addCell(new PdfPCell(new Paragraph(rsm.getColumnName(i),font_bold)));
            }
            
            while(rs.next()) {
                RowCount ++;
                for(int j=1; j<=col; j++) {
                    table.addCell(new Paragraph(rs.getString(j),font_normal));
                }

            }
            rs.close();
            stmt.close();
            cn.close();
            
            table.setWidthPercentage(100);
            
            //Logo Azienda
            
            ServletContext context = getServletContext();

            String path = context.getRealPath("/img/logo_pdf.gif");
            
            Image logo_pdf = Image.getInstance(Image.getInstance(path));
            logo_pdf.scaleAbsolute(500, 60);

            
            //document.add(Image.getInstance(path).scaleAbsolute(600, 72));
            document.add(logo_pdf);
            
            document.add(new Paragraph(titolo,font_titolo));
            document.add(new Paragraph(" ",font_sottotitolo));
            document.add(new Paragraph("Totale record:" + RowCount,font_sottotitolo));
            document.add(new Paragraph(" ",font_sottotitolo));
            document.add(table);
        } catch (Exception e) {
           System.out.println("<br>"+ e.getMessage());
        }

    document.close();

    }
    
   
}
